//
//  MDataDetailAudioViewController.m
//  Gunz
//
//  Created by Joggy on 16/8/25.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "MDataDetailAudioViewController.h"
#import "SKAudioPlayer.h"
#import "SKPlayerProgressView.h"

@interface MDataDetailAudioViewController () {
    
    SKPlayerProgressView *progressView;
    SKAudioPlayer *player;
    NSTimer *timer;
    
}

@end

@implementation MDataDetailAudioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [player stop];
}

- (void)viewWillLayoutSubviews {
    progressView.center = CGPointMake(screenWidth/2, screenHeight - progressView.frame.size.height/2 - 64);
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = [_dataPath componentsSeparatedByString:@"/"].lastObject;
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    progressView = [[SKPlayerProgressView alloc] init];
    [self.view addSubview:progressView];
    [progressView.slider addTarget:self action:@selector(audioSeek:) forControlEvents:UIControlEventValueChanged];
    [progressView.pauseButton addTarget:self action:@selector(audioPause:) forControlEvents:UIControlEventTouchUpInside];
    [progressView.stopButton addTarget:self action:@selector(audioStop:) forControlEvents:UIControlEventTouchUpInside];
    player = [[SKAudioPlayer alloc] init];
    [player acceptProgress:progressView.slider];
    [player acceptProgressTimeLabel:progressView.progressLabel];
    [player acceptDurationTimeLabel:progressView.durationLabel];
    [player play:_dataPath withAllPath:@[_dataPath]];
    
//    NSLog(@"%@", _dataPath);
}

#pragma mark -- 事件方法
- (void)audioSeek:(UISlider*)slider {
    
}

- (void)audioPause:(UIButton*)button {
    button.selected = !button.selected;
    if(button.selected) {
        [player pause];
    }
    else {
        [player resume];
    }
    NSLog(@"%f", player.duration);
}

- (void)audioStop:(UIButton*)button {
    [player stop];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
